/**
 * @author		Ben Layet
 * @version 	1.0
 * @date		25 Sep 2008
 */
package org.ignoramus.application.store;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * The XML lesson data.
 */
public class XmlLessonParser
{
	/** Title tag. */
	private static final String	title_tag			= "title";

	/** template tag. */
	private static final String	template_tag		= "template";

	/** log tag. */
	private static final String	log_tag				= "log";

	/** cards tag. */
	private static final String	cards_tag			= "cards";

	/** source attribute. */
	private static final String	src_attribute		= "src";

	/** The XML lesson data. */
	private final InputStream	xmlLessonData;

	/** The file name of the XML template file */
	private String				templateFilename	= null;

	/** The file name of the XML results log file */
	private String				logFilename			= null;

	/** The file name of the XML cards file */
	private String				cardsFilename		= null;

	/** The title of the lesson */
	private String				title				= "";

	/**
	 * Constructs from the lesson data.
	 * 
	 * @param xmlLessonData
	 *            The XML lesson data.
	 */
	public XmlLessonParser(InputStream xmlLessonData)
	{
		this.xmlLessonData = xmlLessonData;
	}

	/**
	 * parses the file.
	 * 
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	private void parse() throws StoreException
	{
		// check if it has already been parsed
		if (templateFilename == null)
		{
			// document builder factory
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			try
			{
				// document builder
				DocumentBuilder db = dbf.newDocumentBuilder();
				// parse the document
				Document doc = db.parse(xmlLessonData);
				// normalise the document
				doc.getDocumentElement().normalize();
				// get the title element
				Element element = (Element) doc.getElementsByTagName(title_tag).item(0);
				// set the title
				title = element.getTextContent();
				// get the template element
				element = (Element) doc.getElementsByTagName(template_tag).item(0);
				// set the template filename
				templateFilename = element.getAttribute(src_attribute);
				// get the log element
				element = (Element) doc.getElementsByTagName(log_tag).item(0);
				// set the log filename
				logFilename = element.getAttribute(src_attribute);
				// get the cards element
				element = (Element) doc.getElementsByTagName(cards_tag).item(0);
				// set the cards filename
				cardsFilename = element.getAttribute(src_attribute);
				
				//close the input stream
				xmlLessonData.close();

			}
			catch (ParserConfigurationException e)
			{
				throw new StoreException("Cannot parse the lesson file.", e);
			}
			catch (SAXException e)
			{
				throw new StoreException("Cannot parse the lesson file.", e);
			}
			catch (IOException e)
			{
				throw new StoreException("Cannot parse the lesson file.", e);
			}
		}
	}

	/**
	 * @returns the file name of the XML template file.
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	public String getTemplateFilename() throws StoreException
	{
		parse();
		return templateFilename;
	}

	/**
	 * @returns The file name of the XML cards file.
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	public String getCardsFilename() throws StoreException
	{
		parse();
		return cardsFilename;
	}

	/**
	 * @returns The file name of the XML results log file.
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	public String getLogFilename() throws StoreException
	{
		parse();
		return logFilename;
	}

	/**
	 * @returns The title of the lesson.
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	public String getTitle() throws StoreException
	{
		parse();
		return title;
	}
}
